package com.swz.dcrm.ui.car;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.CarAdapter;
import com.swz.dcrm.model.Car;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.wrapper.EmptyWrapper;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CarResItemFragment extends BaseFragment {

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;
    @Inject
    CarManagementViewModel carManagementViewModel;


    private CarAdapter carAdapter;
    private String searchTag;
    private EmptyWrapper emptyWrapper;

    private int page=1;
    private int size=15;
    private long total;
    public static CarResItemFragment newInstance(int status) {
        CarResItemFragment fragment=new CarResItemFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("status",status);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    public void onLoadRetry() {
        int status=getArguments().getInt("status");
        carManagementViewModel.getCars(status,page,size,searchTag)
                .observe(this,observer);
    }
    Observer<PageResponse<Car>>observer=new Observer<PageResponse<Car>>() {
        @Override
        public void onChanged(PageResponse<Car> carPageResponse) {

            smartRefreshLayout.finishLoadmore();
            smartRefreshLayout.finishRefresh();

            if(carPageResponse.isSuccess()){
                total=carPageResponse.getTotal();
                if(carAdapter==null){
                    carAdapter=new CarAdapter(getContext(),carPageResponse.getData());
                    emptyWrapper=getEmptyWrapper(carAdapter,R.mipmap.empty_showroom);
                    rv.setAdapter(emptyWrapper);
                }else {
                    carAdapter.refresh(carPageResponse.getPage(),carPageResponse.getData());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public int layoutId() {
        return R.layout.fragment_car_res_item;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(), 0, 10, 10, 10));
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if (carAdapter != null && carAdapter.getItemCount() >= total) {

                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            } else {
                page++;
                onLoadRetry();
            }
        });
        carManagementViewModel.getCarNameInWareHouse().observe(this, s -> {
            this.searchTag=s;
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        });
        onLoadRetry();
        return false;
    }



}
